package Snapshot;

import Bean.*;

import java.util.ArrayList;

public class SnapshotManager {

    public static void createSnapshotOfReback(Integer id,MindMap target){
        MindMapTemp temp = createMindMapTemp(target);
        SnapshotStack.storeSnapshotOfReback(id,temp);
    }

    public static MindMapTemp getSnapshotOfReback(Integer id){
        MindMapTemp temp = SnapshotStack.getSnapshotOfReback(id);
        return temp;
    }

    public static MindMapTemp getSnapshotOfNext(Integer id){
        MindMapTemp temp = SnapshotStack.getSnapshotOfNext(id);
        return temp;
    }

    private static MindMapTemp createMindMapTemp(MindMap target){
        MindMapTemp temp = new MindMapTemp();
        MindMapNode center = target.getCenter();
        temp.setCenter(new MindMapNodeTemp(center.getTextProperty(),center.getX(),center.getY()));
        initTempOfNode(temp.getCenter(),center);
        initTempOfLine(temp,target);
        return temp;
    }

    private static void initTempOfNode(MindMapNodeTemp temp,MindMapNode target){
        ArrayList<MindMapNode> nodes = target.getChildNode();
        for(MindMapNode node : nodes){
            MindMapNodeTemp nextTemp = new MindMapNodeTemp(node.getTextProperty(),node.getX(),node.getY());
            nextTemp.setParent(new MindMapNodeTemp(node.getParentNode().getTextProperty(),node.getParentNode().getX(),node.getParentNode().getY()));
            temp.getNodes().add(nextTemp);
            initTempOfNode(nextTemp,node);
        }
    }

    private static void initTempOfLine(MindMapTemp temp,MindMap target){
        for(MindMapLine line : target.getLines()){
            MindMapLineTemp lineTemp = new MindMapLineTemp(line.getBeginX(),line.getBeginY(),line.getEndOfX(),line.getEndOfY());
            lineTemp.setBeginNode(new MindMapNodeTemp(line.getBeginNode().getTextProperty(),line.getBeginNode().getX(),line.getBeginNode().getY()));
            lineTemp.setEndNode(new MindMapNodeTemp(line.getEndNode().getTextProperty(),line.getEndNode().getX(),line.getEndNode().getY()));
            lineTemp.setBegin(line.getBeginNode());
            lineTemp.setEnd(line.getEndNode());
            temp.getLines().add(lineTemp);
        }
    }

}
